//package com.star.common.monitor.application.service;
//
//import cn.hutool.core.text.CharSequenceUtil;
//import com.star.common.core.contract.exception.ServiceException;
//import com.star.common.core.utils.ExceptionKit;
//import com.star.common.monitor.domain.code.model.CodeVersion;
//import com.star.common.monitor.domain.external.send.MonitorSender;
//import com.star.common.monitor.infras.config.SpringProperties;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.boot.context.event.ApplicationFailedEvent;
//import org.springframework.boot.context.event.ApplicationStartedEvent;
//import org.springframework.context.ApplicationEvent;
//import org.springframework.context.ApplicationListener;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import java.io.IOException;
//import java.util.Collections;
//import java.util.Properties;
//
///**
// * 代码版本通知
// *
// * @author zhouhengzhe
// */
//@Slf4j
//@Service
//@RequiredArgsConstructor
//public class CodeVersionService implements ApplicationListener<ApplicationEvent> {
//    public static CodeVersion CODE_VERSION = null;
//
//    private final MonitorSender sender;
//
//    private final SpringProperties springProperties;
//
//    @Override
//    public void onApplicationEvent(ApplicationEvent event) {
//        if (!(event instanceof ApplicationStartedEvent || event instanceof ApplicationFailedEvent)) {
//            return;
//        }
//        try {
//            Properties p = new Properties();
//            p.load(this.getClass().getClassLoader().getResourceAsStream("git.properties"));
//            CODE_VERSION = new CodeVersion(p.getProperty("git.branch"), p.getProperty("git.build.time"),
//                    p.getProperty("git.build.version"),
//                    p.getProperty("git.commit.id"), p.getProperty("git.commit.message.full"),
//                    p.getProperty("git.commit.user.name"), p.getProperty("git.commit.time"));
//            log.info("代码版本 => {}", CODE_VERSION);
//            String projectStackTrace = null;
//            StringBuilder sb = new StringBuilder();
//            if (event instanceof ApplicationStartedEvent) {
//                sb.append(CharSequenceUtil.format("应用 **{}** 启动成功！", springProperties.getName())).append("\n");
//            } else {
//                ApplicationFailedEvent failedEvent = (ApplicationFailedEvent) event;
//                projectStackTrace = ExceptionKit.getProjectStackTraces(failedEvent.getException());
//                sb.append(CharSequenceUtil.format("应用 **{}** 启动失败！", springProperties.getName())).append("\n");
//            }
//            this.markdownTextAppend(sb, "提交ID", CODE_VERSION.getCommitId());
//            this.markdownTextAppend(sb, "提交用户", CODE_VERSION.getCommitUser());
//            this.markdownTextAppend(sb, "提交信息", CODE_VERSION.getCommitMessage());
//            this.markdownTextAppend(sb, "提交时间", CODE_VERSION.getCommitTime());
//            if (projectStackTrace != null) {
//                this.markdownTextAppend(sb, "StackTrace", projectStackTrace);
//            }
//            sender.send(sb.toString(), Collections.emptyList(), Boolean.FALSE);
//        } catch (IOException e) {
//            throw new ServiceException(e);
//        }
//    }
//
//    private void markdownTextAppend(StringBuilder sb, String key, String value) {
//        if (StringUtils.hasText(value)) {
//            sb.append("**").append(key).append(":** ").append(value).append("\n");
//        }
//    }
//}
